{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## GloVe embedding \n",
    "\n",
    "Download GloVe pre-trained embedding glove.6B.zip from: https://nlp.stanford.edu/projects/glove/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded 400000 word vectors.\n"
     ]
    }
   ],
   "source": [
    "# load the whole embedding into memory\n",
    "embeddings = dict()\n",
    "f = open('glove.6B.200d.txt')\n",
    "for line in f:\n",
    "    values = line.split()\n",
    "    word = values[0]\n",
    "    embedding = np.array(values[1:], dtype='float32')\n",
    "    embeddings[word] = embedding\n",
    "f.close()\n",
    "print('Loaded %s word vectors.' % len(embeddings))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = embeddings['was']\n",
    "# print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = embeddings['is']\n",
    "# print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "z = embeddings['are']\n",
    "# print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Similarity:  20.964674 Prediction:  the\n",
      "Similarity:  21.173893 Prediction:  to\n",
      "Similarity:  23.63844 Prediction:  and\n",
      "Similarity:  29.421112 Prediction:  was\n",
      "Similarity:  29.430035 Prediction:  be\n",
      "Similarity:  30.773361 Prediction:  are\n",
      "Similarity:  33.251774 Prediction:  have\n",
      "Similarity:  44.052734 Prediction:  were\n"
     ]
    }
   ],
   "source": [
    "x = np.subtract(x, y)\n",
    "x = np.add(x, z)\n",
    "predict = None\n",
    "max_similarity = 0\n",
    "for word, embedding in embeddings.items():\n",
    "    y = np.array(embedding)\n",
    "    similarity = np.dot(x, y)\n",
    "    if similarity > max_similarity:\n",
    "        predict = word\n",
    "        print(\"Similarity: \", similarity, \"Prediction: \", word)\n",
    "        max_similarity = similarity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
